// default for all commands
Refresh.Web.DefaultCommand = {
	// default type
	type: 'button',
	
	// for toolbarItems that have items, if they are not specified, this value will be used
	defaultItems: [],
	
	// default settings for objects like dropdowns
	defaultSettings: {},
	
	// returns what the state of the command is
	getValue: function(ftb) {
		var value = '';
		
		if (ftb != null) {
			value =  ftb.queryCommandState(this.name);
		} else {
			value =  null;
		}
					
		return value;
	},
	
	// this is the default execute which uses the executeCommand method
	execute: function(ftb, value, src) {
			
		if (ftb != null) {
			ftb.executeCommand(this.name.toLowerCase(), value);
		}
	}
};

// stores all commands available to the editor
Refresh.Web.EditorCommands = {
	_commands: {},

	initialize: function() {
	},
	
	// Creates a ToolbarItem with this command
	createItem: function(id, name, settings, items, toolbar) {
		
		var command = this.getCommand(name);
		
		if (command == null) {
			
			return null;
		}
		
		// create the visual object (button, combobox, etc.) for the command
		var item = null;
		switch (command.type.toLowerCase()) {
			case 'button':
				item = new Refresh.Web.Button(id, command, settings, items, toolbar);
				break;
			case 'gallerybutton':
				item = new Refresh.Web.GalleryButton(id, command, settings, items, toolbar);
				break;
			case 'combobox':
				item = new Refresh.Web.ComboBox(id, command, settings, items, toolbar);
				break;
			case 'togglebutton':
				item = new Refresh.Web.ToggleButton(id, command, settings, items, toolbar);
				break;				
		}
		
		return item;
	},

	addCommand: function(command) {
		var _command = Object.extend({}, Refresh.Web.DefaultCommand);
		this._commands[command.name] = Object.extend(_command, command);
	},
	
	getCommand: function(name) {
		return this._commands[name];
	},	
	
	execute: function(ftb,name,value) {	
		window.parent.Refresh.Web.CurrentFreeTextBox = ftb;	
		this.getCommand(name).execute(ftb,name,value);
	}, 
	
	getValue: function(ftb,name) {
		return this.getCommand(name).getValue(ftb,name);
	}
	
};

Refresh.Web.QueryMethods = {
	alwaysOn: function() { return FTB_BUTTONSTATE_NORMAL; },
	
	isInsideTag: function(ftb, tagName) {
		var tag = ftb.getNearest(tagName); return (tag==null) ? FTB_BUTTONSTATE_DISABLED : FTB_BUTTONSTATE_NORMAL;
	}
};

Refresh.Web.ListItemBuilder = {
	createFontNamesList: function(items) { 
		var formattedItems = [];		
		for (var i=0; i<items.length; i++)
			formattedItems.push({text: items[i], value: items[i], display: '<span style="font-family:' + items[i] + '">' + items[i] + '</span>'});		
		return formattedItems;
	},
	createFontSizesList: function(items) { 
		var formattedItems = [];		
		for (var i=0; i<items.length; i++) {
			var item = items[i].toString();
			formattedItems.push({text: item, value: item, display: '<font size="' + item + '">' + item + '</font>'});
		}
		return formattedItems;
	},
	createInsertHtml: function(items) { 
		var formattedItems = [];		
		for (var i=0; i<items.length; i++) {
			var item = items[i].toString();
			formattedItems.push({text: '', value: item, display: item});
		}
		return formattedItems;
	}			
};

// BASIC COMMANDS

Refresh.Web.EditorCommands.addCommand({name:'Bold'});
Refresh.Web.EditorCommands.addCommand({name:'Italic'});
Refresh.Web.EditorCommands.addCommand({name:'Underline'});
Refresh.Web.EditorCommands.addCommand({name:'Strikethrough'});
Refresh.Web.EditorCommands.addCommand({name:'Superscript'});
Refresh.Web.EditorCommands.addCommand({name:'Subscript'});
Refresh.Web.EditorCommands.addCommand({name:'Indent'});
Refresh.Web.EditorCommands.addCommand({name:'Outdent',getValue:Refresh.Web.QueryMethods.alwaysOn});
Refresh.Web.EditorCommands.addCommand({name:'Indent',getValue:Refresh.Web.QueryMethods.alwaysOn});
Refresh.Web.EditorCommands.addCommand({name:'JustifyLeft'});
Refresh.Web.EditorCommands.addCommand({name:'JustifyRight'});
Refresh.Web.EditorCommands.addCommand({name:'JustifyCenter'});
Refresh.Web.EditorCommands.addCommand({name:'JustifyFull'});
Refresh.Web.EditorCommands.addCommand({name:'Unlink', getValue:function(ftb) { return Refresh.Web.QueryMethods.isInsideTag(ftb,'a'); }  });

// ???
Refresh.Web.EditorCommands.addCommand({name:'Cut',execute:function(ftb) {ftb.cut();},getValue:Refresh.Web.QueryMethods.alwaysOn });
Refresh.Web.EditorCommands.addCommand({name:'Copy',execute:function(ftb) {ftb.copy();},getValue:Refresh.Web.QueryMethods.alwaysOn });
Refresh.Web.EditorCommands.addCommand({name:'Paste',execute:function(ftb) {ftb.paste();},getValue:Refresh.Web.QueryMethods.alwaysOn });
Refresh.Web.EditorCommands.addCommand({name:'Clear',execute:function(ftb) {ftb.setHtml('');ftb.focus();},getValue:Refresh.Web.QueryMethods.alwaysOn });

// name changes
Refresh.Web.EditorCommands.addCommand({name:'InsertRule',execute:function(ftb) {ftb.executeCommand('inserthorizontalrule');},getValue:Refresh.Web.QueryMethods.alwaysOn });
Refresh.Web.EditorCommands.addCommand({name:'NumberedList',execute:function(ftb) {ftb.executeCommand('insertorderedlist');},getValue:Refresh.Web.QueryMethods.alwaysOn });
Refresh.Web.EditorCommands.addCommand({name:'BulletedList',execute:function(ftb) {ftb.executeCommand('insertunorderedlist');},getValue:Refresh.Web.QueryMethods.alwaysOn });

// custom buttons with methods in FreeTextBox


Refresh.Web.EditorCommands.addCommand({name:'Undo',execute:function(ftb) {ftb.undo();},getValue:function(ftb) { return (ftb.undoIndex > 0) ? FTB_BUTTONSTATE_NORMAL : FTB_BUTTONSTATE_DISABLED;}  });
Refresh.Web.EditorCommands.addCommand({name:'Redo',execute:function(ftb) {ftb.redo();},getValue:function(ftb) { return (ftb.undoIndex > -1 && ftb.undoIndex < ftb.undoSteps.length-1) ? FTB_BUTTONSTATE_NORMAL : FTB_BUTTONSTATE_DISABLED;}  });
Refresh.Web.EditorCommands.addCommand({name:'Print',execute:function(ftb) {ftb.print();},getValue:Refresh.Web.QueryMethods.alwaysOn });
Refresh.Web.EditorCommands.addCommand({name:'Save',execute:function(ftb) {ftb.save();},getValue:Refresh.Web.QueryMethods.alwaysOn });
Refresh.Web.EditorCommands.addCommand({name:'Revert',execute:function(ftb) {ftb.revert();},getValue:Refresh.Web.QueryMethods.alwaysOn });

// totally custom
Refresh.Web.EditorCommands.addCommand({name:'SwitchMode',execute:function(ftb) { if (ftb._mode == FTB_MODE_DESIGN) ftb.goToCodeMode(); else ftb.goToDesignMode() },getValue:function(ftb) { return (ftb._mode == FTB_MODE_DESIGN) ? FTB_BUTTONSTATE_NORMAL  :  FTB_BUTTONSTATE_HIGHLIGHT } });

// special buttons (included in contentEditable spec, but needing revision)
Refresh.Web.EditorCommands.addCommand({name:'InsertImage',execute:function(ftb) { 
	Refresh.Web.ModalWindow.show(Refresh.windowPath + 'image.html',400,250,null);
},getValue:Refresh.Web.QueryMethods.alwaysOn});
Refresh.Web.EditorCommands.addCommand({name:'CreateLink',execute:function(ftb) { 
	Refresh.Web.ModalWindow.show(Refresh.windowPath + 'link.html',400,160,null);
},getValue:Refresh.Web.QueryMethods.alwaysOn});

// main combo boxes
Refresh.Web.EditorCommands.addCommand({name:'FontFacesList',type:'combobox',
		execute:function(ftb,value) { ftb.executeCommand('fontname', value); }, 
		getValue:function(ftb) { return ftb.queryCommandValue('fontname'); }, 
		defaultSettings: {title: 'Font',width: '70px'},
		defaultItems: Refresh.Web.ListItemBuilder.createFontNamesList(['Arial','Courier','Georgia','Tahoma','Times','Verdana']) });
Refresh.Web.EditorCommands.addCommand({name:'FontSizesList',type:'combobox',
		execute:function(ftb,value) { ftb.executeCommand('fontsize', value); }, 
		getValue:function(ftb) { return ftb.queryCommandValue('fontsize'); }, 
		defaultSettings: {title: 'Size',width: '35px'},
		defaultItems: Refresh.Web.ListItemBuilder.createFontSizesList([1,2,3,4,5,6]) });
Refresh.Web.EditorCommands.addCommand({name:'ParagraphMenu',type:'combobox',
		execute:function(ftb,value) { ftb.executeCommand('formatBlock', value); }, 
		getValue:function(ftb) { return ftb.queryCommandValue('formatBlock'); }, 
		defaultSettings: {title: 'Format',width: '100px',listWidth:'150px'},
		defaultItems: [{text:'Normal',value:'<p>'}, {text:'Heading 1',value:'<h1>',display:'<h1>Heading 1</h1>'}, {text:'Heading 2',value:'<h2>',display:'<h2>Heading 2</h2>'}, {text:'Heading 3',value:'<h3>',display:'<h3>Heading 3</h3>'}, {text:'Heading 4',value:'<h4>',display:'<h4>Heading 4</h4>'}, {text:'Preformatted',value:'<pre>',display:'<pre>Preformatted</pre>'}, {text:'Address',value:'<address>',display:'<address>Address</address>'}]} );
